#coding:utf-8
import random
import MySQLdb
import time
import functools
import random
import numpy as np
import matplotlib.pyplot as plt

"""
@author:自之梦 10061661
@time: 17/10/26 上午10:26
#sku_message的商品用于双11普通商家、良品铺子商家、沃天下良品铺子商家
#10个随机选N个不重复的SKU返回

"""


def sku_message(len_str=5):
    sku_message = [{"itemCode":"test_double_11_1", "itemName":"test_double_11_1"},
                   {"itemCode":"test_double_11_2", "itemName":"test_double_11_2"},
                   {"itemCode":"test_double_11_3", "itemName":"test_double_11_3"},
                   {"itemCode":"test_double_11_4", "itemName":"test_double_11_4"},
                   {"itemCode":"test_double_11_5", "itemName":"test_double_11_5"},
                   {"itemCode":"test_double_11_6", "itemName":"test_double_11_6"},
                   {"itemCode":"test_double_11_7", "itemName":"test_double_11_7"},
                   {"itemCode":"test_double_11_8", "itemName":"test_double_11_8"},
                   {"itemCode":"test_double_11_9", "itemName":"test_double_11_9"},
                   {"itemCode":"test_double_11_10", "itemName":"test_double_11_10"}]
    new_sku_message = []
    
    while True:
        get_sku_mes = sku_message[random.randint(1, 9)]       
        if get_sku_mes not in new_sku_message:
            new_sku_message.append(get_sku_mes)
            
        if len(new_sku_message) == len_str:
            break      
    
    return new_sku_message


def fn_time(fuc):
    @functools.wraps(fuc)
    def function_time(*args, **kwargs):
        t0 = time.time()
        result = fuc(*args, **kwargs)
        t1 = time.time()
        Use_time = int(float( '%.3f' % (t1 - t0))*1000)
        print ("Total time running %s: %sms" %
               (fuc.func_name, str(Use_time))
               )
        return result
    return function_time


def log(fuc):
    @functools.wraps(fuc)
    def wrapper(*args, **kwargs):
        print "-"*32,"\n"
        print "%s fuc start ..." % fuc.__name__
        t1 = time.time()
        result = fuc(*args, **kwargs)
        t2 = time.time()
        time_Val = int(float( '%.3f' % (t2 - t1))*1000)
        print "%s fuc end ..." % fuc.__name__
        print "{1} fuc total running :{0}ms".format(str(time_Val), fuc.__name__)
        print "-"*32
        return result
    return wrapper


@log
#@fn_time
def get_sku_for_Db():
    
    print "start..."
    db = MySQLdb.connect(
        host="10.10.111.240",
        port = 3306,
        user = "omsmanger",
        passwd = "alog@123",
        db = "oms",
        charset='utf8'
        )
    cursor = db.cursor()

    #随机获取ljj(ljj的merchant_id是1)商家10个SKU
    sql = "SELECT * FROM `goods_sku` WHERE id >= (SELECT floor(RAND() * (SELECT MAX(id) FROM `goods_sku`))) AND merchant_id=1 ORDER BY id LIMIT 10;"
    sql2 = "SELECT * \
FROM `goods_sku` AS t1 JOIN (SELECT ROUND(RAND() * ((SELECT MAX(id) FROM `goods_sku`)-(SELECT MIN(id) FROM `goods_sku`))+(SELECT MIN(id) FROM `goods_sku`)) AS id) AS t2 \
WHERE t1.id >= t2.id \
AND merchant_id=1 \
ORDER BY t1.id LIMIT 10;"
    
    cursor.execute(sql2)
    result = cursor.fetchall()#获取查询结果 
    cursor.close()
    db.close()

    sku_messages = []

    if result:
        for i in result:
            sku_message = {}
            sku_message["itemCode"] = i[3]
            sku_message["itemName"] = i[4]
            sku_messages.append(sku_message)

        print "...end"
        return sku_messages
    else:
        print "...end1"
        return False


def result_for_chart():
    x = [0,1,2,3]  
    y = [0,4,2,5,4]  

    plt.plot(x,y)

    plt.xlabel("action1")
    plt.ylabel(u"time(m/s)")
    plt.title("tps")

    plt.xlim(0.0, 9.0)
    return plt.show()


if __name__ == "__main__":
    result = get_sku_for_Db()
    print result 
   


    
